var express = require('express');
var router = express.Router();
var mysql = require('mysql');
const { mysqlCreate } = require('./utils');

//连接数据库
let conn = mysqlCreate();

//获取所有文章
router.get('/', (req, res) => {
  conn = mysql.createConnection(conn.config);
  var sql = `SELECT * FROM user`;
  //连接数据库
  conn.connect();
  conn.query(sql, function (err, result) {
    if(err){
      console.log('[SELECT ERROR] - ',err.message);
      res.json({ code: -1})
    }else{
      res.json({ code: 10000, message: "OK", data: result})
    } 
  });
  conn.end();
})
//获取所有文章
// router.get('/', (req, res) => {
//   console.log(req.params)
//   conn = mysql.createConnection(conn.config); 
//   var sql = 'SELECT * FROM article where type';
//   conn.connect();
//   conn.query(sql, function (err, result) {
//     if(err){
//       console.log('[SELECT ERROR] - ',err.message);
//       res.json({ code: -1})
//     }else{
//       res.json({ code: 10000, message: "OK", data: result})
//       console.log(req.params + "=====")
//     } 
//   });
//   conn.end();
// })

//验证token
function verifyToken(req, res, next) {
  const bearerHeader = req.headers['authorization'];
  if(typeof bearerHeader !== 'undefined') {
    const bearer = bearerHeader.split(' ');
    const bearerToken = bearer[1];
    // req.token = bearerToken;
    console.log(bearerToken)
  } else {
    res.sendStatus(403);
  }
}

module.exports = router;
